//
//  EditShopImageController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "EditShopImageController.h"

@interface EditShopImageController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScroll;
@property (nonatomic, strong) NSString *headUrl;
@property (nonatomic, strong) NSString *bottomUrl;
@property (nonatomic, strong) NSString *indexUrl;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) NSArray *arrray;
@property (nonatomic, strong) NSString *mainUrl;
@end

@implementation EditShopImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviWith:@"店铺图片"];
    self.view.backgroundColor = UIColorFromHex(0xF7F7F7);
    [self setMainView];
}

- (void)setMainView
{
    self.arrray = [self.realUrl componentsSeparatedByString:@","];
    self.mainScroll = [UIScrollView new];
    if (@available(iOS 11.0, *)) {
        self.mainScroll.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, SCREENHEIGHT-TopBarHeight_xs);
    } else {
        // Fallback on earlier versions
        self.mainScroll.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight);
    }
    self.mainScroll.delegate = self;
    self.mainScroll.backgroundColor = ClearColor;
    self.mainScroll.contentSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT);
    [self.view addSubview:self.mainScroll];
    
    UIImageView *headIV = [UIImageView new];
    CGFloat imageHeight = 194*(SCREENWIDTH-30)/345;
    headIV.frame = CGRectMake(15, 0, SCREENWIDTH-30, imageHeight);
    headIV.backgroundColor = [UIColor clearColor];
    headIV.tag = 88;
    headIV.contentMode = UIViewContentModeScaleAspectFill;
    [headIV sd_setImageWithURL:[NSURL URLWithString:[_arrray firstObject]]placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
    [self.mainScroll addSubview:headIV];
    
    UIView *whiteView = [UIView new];
    whiteView.frame = CGRectMake(0, headIV.bottom, SCREENWIDTH, 55);
    whiteView.backgroundColor = ClearColor;
    [self.mainScroll addSubview:whiteView];
    
    UILabel *headLab = [UILabel new];
    headLab.frame = CGRectMake(headIV.left, 14, 65, 18);
    headLab.textColor = UIColorFromHex(0x020200);
    headLab.font = FontWithSize(14);
    headLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1.17 and:@"门头图片"];
    [whiteView addSubview:headLab];
    
    UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    headBtn.frame = CGRectMake(SCREENWIDTH-70, 10, 55, 25);
    headBtn.layer.cornerRadius = 13;
    headBtn.layer.masksToBounds = YES;
    headBtn.backgroundColor = goldColor;
    headBtn.titleLabel.font = FontWithSize(12);
    [headBtn setTitleColor:UIColorFromHex(0x020200) forState:UIControlStateNormal];
    headBtn.tag = 50;
    [headBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"修改"] forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(editImage:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:headBtn];
    
    if (_arrray.count > 1)
    {
        UIImageView *bottomIV = [UIImageView new];
        bottomIV.frame = CGRectMake(15, whiteView.bottom, SCREENWIDTH-30, imageHeight);
        bottomIV.backgroundColor = [UIColor clearColor];
        bottomIV.tag = 89;
        bottomIV.contentMode = UIViewContentModeScaleAspectFill;
        [bottomIV sd_setImageWithURL:[NSURL URLWithString:[_arrray lastObject]]placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
        [self.mainScroll addSubview:bottomIV];
        
        UIView *bottomwView = [UIView new];
        bottomwView.frame = CGRectMake(0, bottomIV.bottom, SCREENWIDTH, 50);
        bottomwView.backgroundColor = TotalWhite;
        self.bottomView = bottomwView;
        [self.mainScroll addSubview:bottomwView];
        
        UILabel *bottomLab = [UILabel new];
        bottomLab.frame = CGRectMake(headIV.left, 14, 65, 18);
        bottomLab.textColor = UIColorFromHex(0x020200);
        bottomLab.font = FontWithSize(14);
        bottomLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1.17 and:@"店内图片"];
        [bottomwView addSubview:bottomLab];
        
        UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        bottomBtn.frame = CGRectMake(SCREENWIDTH-70, 10, 55, 25);
        bottomBtn.layer.cornerRadius = 13;
        bottomBtn.layer.masksToBounds = YES;
        bottomBtn.backgroundColor = goldColor;
        bottomBtn.titleLabel.font = FontWithSize(12);
        [bottomBtn setTitleColor:UIColorFromHex(0x020200) forState:UIControlStateNormal];
        bottomBtn.tag = 51;
        [bottomBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"修改"] forState:UIControlStateNormal];
        [bottomBtn addTarget:self action:@selector(editImage:) forControlEvents:UIControlEventTouchUpInside];
        [bottomwView addSubview:bottomBtn];
    }
    
    CGFloat padTop = self.bottomView?_bottomView.bottom+30:whiteView.bottom+30;
    UIView *footView = [UIView new];
    footView.backgroundColor = ClearColor;
    footView.frame = CGRectMake(0,padTop, SCREENWIDTH, 40);
    [self.mainScroll addSubview:footView];
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(40, 0, SCREENWIDTH-80, 40);
    sureBtn.backgroundColor = goldColor;
    [sureBtn setTitleColor:titleColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FontWithSize(18);
    [sureBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:2 and:@"保存"] forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = 15;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:sureBtn];
}

#pragma mark -- 保存图片
- (void)sure:(UIButton *)btn
{
    if (self.headUrl.length == 0 && self.bottomUrl.length == 0) {
        [self ShowHUDWithMessage:@"暂无修改图片，无需上传"];
        return;
    }
    else
    {
        if (self.headUrl.length > 0 && self.bottomUrl.length>0) {
            self.mainUrl = [NSString stringWithFormat:@"%@,%@",self.headUrl,self.bottomUrl];
        }
        else if (self.headUrl.length > 0)
        {
            if (self.arrray.count>1) {
                self.mainUrl = [NSString stringWithFormat:@"%@,%@",self.headUrl,self.arrray[1]];
            }
            else
            {
                 self.mainUrl = [NSString stringWithFormat:@"%@",self.headUrl];
            }
        }
        else if (self.bottomUrl.length>0)
        {
            if (self.arrray.count>1) {
                self.mainUrl = [NSString stringWithFormat:@"%@,%@",self.arrray[0],self.bottomUrl];
            }
            else
            {
                self.mainUrl = [NSString stringWithFormat:@"%@",self.bottomUrl];
            }
        }
        NSLog(@"mainUrl = %@",self.mainUrl);
        [self editNewImage:self.mainUrl];
    }
}

- (void)editImage:(UIButton *)btn
{
    UIImageView *headIV = (UIImageView *)[self.view viewWithTag:88];
    UIImageView *bottomIV = (UIImageView *)[self.view viewWithTag:89];
    __weak __typeof(self) weakSelf = self;
    if (btn.tag == 50)
    {
        [self takePhoto];
        self.photoBlock = ^(UIImage * _Nonnull image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                headIV.image = image;
                weakSelf.headUrl = [weakSelf uploadImageUrl:image];
            });
        };
    }
    else
    {
        [self takePhoto];
        self.photoBlock = ^(UIImage * _Nonnull image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                bottomIV.image = image;
                weakSelf.bottomUrl = [weakSelf uploadFootImageUrl:image];
            });
        };
    }
}

#pragma mark -- 上传图片
- (NSString *)uploadImageUrl:(UIImage *)image
{
    UIImage *imageNew = [TTKUtil imageWithImageSimple:image scaledToSize:CGSizeMake(600, 400)];
    NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.5);
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:imageData forKey:@"file"];
    __weak __typeof(self) weakSelf = self;
    [[HttpRequest currentHttpRequest]uploadWithParams:dict andImageData:imageData block:^(NSString *SamllPhoto) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.headUrl = [NSString stringWithFormat:@"%@",SamllPhoto];
        });
    }];
    return weakSelf.headUrl;
}

- (NSString *)uploadFootImageUrl:(UIImage *)image
{
    UIImage *imageNew = [TTKUtil imageWithImageSimple:image scaledToSize:CGSizeMake(600, 400)];
    NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.5);
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:imageData forKey:@"file"];
    __weak __typeof(self) weakSelf = self;
    [[HttpRequest currentHttpRequest]uploadWithParams:dict andImageData:imageData block:^(NSString *SamllPhoto) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.bottomUrl = [NSString stringWithFormat:@"%@",SamllPhoto];
        });
    }];
    return weakSelf.bottomUrl;
}

- (void)editNewImage:(NSString *)url
{
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setValue:[NSNumber numberWithInteger:self.id] forKey:@"id"];
    [data setValue:url forKey:@"shopImage"];
    [HttpRequest formPostPath:EditMyInfo_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
        [self ShowHUDWithMessage:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

@end
